﻿


using LaserMark.DataAccess.Models;
using System.Security.Cryptography;

namespace LaserMark.DataAccess.Service;

public class SecretService : ISecretService
{
    private IDBManager DBManager { get; set; }

    public SecretService(IDBManager dBManager)
    {
        DBManager = dBManager;
    }

    public async Task<bool> GetSecret(string type, string password)
    {
        var str = "U2hpQ2h1YW5n";
        using (var db = DBManager.Create())
        {
            using (var md5 = MD5.Create())
            {
                var bytes = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password + str), 0, password.Length);
                var pwd = Convert.ToBase64String(bytes);
                return (await db.FetchAsync<Secret>()).Where(s => s.Type == type && s.Password == pwd).Count() > 0;
            }
        }
    }

    public async Task<bool> CreateSecret(Secret secret)
    {
        var str = "U2hpQ2h1YW5n";
        using var db = DBManager.Create();
        using (var md5 = MD5.Create())
        {
            var bytes = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(secret.Password + str), 0,
                secret.Password.Length);
            secret.Password = Convert.ToBase64String(bytes);
            return (await db.InsertAsync(secret)) != null;
        }
    }
}
